package mes

import (
	"context"
	"encoding/json"
	"ez/apps/mes/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type CheckResult struct {
	mgo.BaseDoc `bson:"-" json:"-"`
	Id          int64 `json:"id" bson:"id,omitempty"`

	Check *Check `json:"check" bson:"check"` //检验记录

	CheckId int64 `json:"checkId" bson:"checkId"` //检验记录

	Tag *CheckTag `json:"tag" bson:"tag"` //检出原因

	TagId int64 `json:"tagId" bson:"tagId"` //检出原因

	Num float64 `json:"num" bson:"num"` //数量

	Group *CheckResultGroup `json:"group" bson:"group"` //检验结果分组

	GroupId int64 `json:"groupId" bson:"groupId"` //检验结果分组

	CreateAt time.Time  `json:"createAt" bson:"createAt"`
	UpdateAt time.Time  `json:"updateAt" bson:"updateAt"`
	DeleteAt *time.Time `json:"deleteAt" bson:"deleteAt"`
}

func (this *CheckResult) DocName() string { return "CheckResult" }

func (this *CheckResult) GetId() int64 { return this.Id }

func (this *CheckResult) SetId(id int64) { this.Id = id }

func (this *CheckResult) Create() error {
	return this.GetFactory().Create(this)
}

func (this *CheckResult) Replace() error {
	return this.GetFactory().Replace(this)
}

func (this *CheckResult) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *CheckResult) Delete() error {
	return this.GetFactory().Delete(this)
}

func (this *CheckResult) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *CheckResult) Destroy() error {
	return this.GetFactory().Destroy(this)
}

func (this *CheckResult) ToString() string {
	return string(this.ToBytes())
}

func (this *CheckResult) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *CheckResult) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *CheckResult) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}

func (this *CheckResult) LoadCheck() {
	if this.CheckId == 0 {
		return
	}
	this.Check, _ = NewCheckCrud().FindId(this.CheckId)
}

func (this *CheckResult) LoadTag() {
	if this.TagId == 0 {
		return
	}
	this.Tag, _ = NewCheckTagCrud().FindId(this.TagId)
}

func (this *CheckResult) LoadGroup() {
	if this.GroupId == 0 {
		return
	}
	this.Group, _ = NewCheckResultGroupCrud().FindId(this.GroupId)
}

func (this *CheckResult) ClearRelationsBeforeSave() mgo.Doc {

	this.Check = nil

	this.Tag = nil

	this.Group = nil

	return this
}

func neverUsed_CheckResult() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type CheckResultAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *CheckResult
	Session *ez.Session
}

func NewCheckResultAccessControl(model *CheckResult, action string, session *ez.Session) *CheckResultAccessControl {
	ctrl := &CheckResultAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.CheckResultAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
